package cn.dankal.basiclib.domain;


import android.util.Log;

import java.io.IOException;

import cn.dankal.basiclib.DKUserManager;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author vane
 */
public class StoreTokenInterceptor implements Interceptor {

    private String[] url = {"frontapi/home"};

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
//        originalRequest.newBuilder().header("Content-Type", "application/json;charset=UTF-8").build();
        if (DKUserManager.getUserInfo() == null || !DKUserManager.isLogined()) {
            return chain.proceed(originalRequest);
        } else {
//            if (chain.request().url().url().getPath().contains("frontapi/home")) {
//                return chain.proceed(originalRequest);
//            }

            String accessToken = DKUserManager.getUserInfo().getAccessToken();
            Log.d("StoreTokenInterceptor","accessToken:"+accessToken);
            if (accessToken != null) {
                Request requestAuthorised = originalRequest.newBuilder()
                        .header("Authentication", accessToken)
//                        .header("X-Access-Token", "989717d52fbcdbc16a7a10")
                        .build();
                return chain.proceed(requestAuthorised);
            }
            return chain.proceed(originalRequest);
        }
    }
}
